use 5.010;
use strict; 
use Context;
use Storable;

use NeuronFacFile;


my $ctx = new Context;



sub clear {
	print "\n";
	given (shift){
		when (/^types$/) {
			$ctx->clear_types();
		}
	}
}

sub define {
	print "\n";
	given (shift){
		when (/^type\s+(?<NewTypeDefinition>.+)/) {
			$ctx->define_type($+{NewTypeDefinition});
		}
		when (/^term\s+(?<NewTermDefinition>.+)/) {
			$ctx->define_term($+{NewTermDefinition});
		}
	}
}
sub ls {
	given (shift) {
		when (/ctx/) {
			$ctx->list();
		}
		default {
			$ctx->list();
		}
	}
}

sub conjugate {
	my $verb = shift;
	my $path = "http://www.leconjugueur.com/conjugaison/verbe/$verb.html";
	my $conjugaison = (new NeuronFacFile($path))->getContent();
	$conjugaison =~ /.*?<\/a>Indicatif<\/td>(.*?)<a name="rul">.*/s;
	$conjugaison = $1;
	say $conjugaison
}

sub evalTerm {
	given ( shift ) {
		when (/quit/) {
			exit(0);
		}
		when (/^define\s*(?<defArg>.*)/){ define($+{defArg}); }
		when (/^conjugate\s*(?<defArg>.*)/){ conjugate($+{defArg}); }
		when (/^ls\s*(?<lsArg>.*)/){ ls($+{lsArg}); } 
		when (/^clear\s*(?<Arg>.*)/){ clear($+{Arg}); } 
		when (/^save\s*(?<Arg>.*)/){ store $ctx, $+{Arg}; } 
		when (/^load\s*(?<Arg>.*)/){ $ctx = retrieve $+{Arg}; } 
		default {
			say "whatever";
		}
	}
} 

while (1) {
	print "\n>> ";
	
	my $userInput = <STDIN>;
	chomp ($userInput);
	evalTerm($userInput);
	
}

